package com.lottery.game.controller.v2.game;

import com.lottery.game.common.Response;
import com.lottery.game.common.utils.StringUtil;
import com.lottery.game.core.model.dto.game.CurrentGameIssue;
import com.lottery.game.core.model.dto.game.GameTypePlayingRelationTO;
import com.lottery.game.core.model.dto.game.GameTypeTO;
import com.lottery.game.core.modules.service.game.GameTypePlayingRelationService;
import com.lottery.game.core.modules.service.game.SysDictService;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * @author : yinlin
 * @version : 1.0
 * @date : 2019-02-18 16:34
 * @Description :
 * @Copyright : Copyright (c) 2018
 * @Company : KeRuYun Technology(Beijing) Chengdu Co. Ltd.
 * @link : com.runshang.apps.cloudgame.controller.v2.game.GameTypePlayingController
 */
@RestController
@RequestMapping("/api/v2/game")
@Api(tags = "APP-首页-获取所有游戏分类接口")
@Slf4j
public class GameTypePlayingController {

    private String imageServer;

    @Autowired
    private SysDictService sysDictService;

    @Autowired
    private GameTypePlayingRelationService gameTypePlayingRelationService;

    @RequestMapping(value = "/type/playings",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "所有游戏分类列表", notes = "所有游戏分类列表")
    public Response<List<GameTypeTO>> playings() {
        List<GameTypeTO> gameTypeTOS = gameTypePlayingRelationService.playings();
        for (GameTypeTO gameTypeTO : gameTypeTOS) {
            for (GameTypePlayingRelationTO relationTO : gameTypeTO.getPlayings()) {
                this.dealGameTypePlayingRelationTO(relationTO);
            }
        }
       return Response.build(gameTypeTOS);
    }

    @RequestMapping(value = "/type/playing",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "gameTypePlayingTicket", value = "此ticket是所有游戏分类列表API中的'游戏玩法编号'字段", required = true, example = "2",dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "roomid", value = "房间编号", required = true, example = "2",dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "获取具体玩法详情", notes = "所有游戏分类玩法详情(ticket是所有游戏分类列表API中的'游戏玩法编号'字段)")
    public Response<GameTypePlayingRelationTO> playing(Integer gameTypePlayingTicket, String roomid) {
        return Response.build(dealGameTypePlayingRelationTO(gameTypePlayingRelationService.findByTicketAndRoomid(gameTypePlayingTicket, roomid)));
    }

    private GameTypePlayingRelationTO dealGameTypePlayingRelationTO(GameTypePlayingRelationTO gameTypePlayingRelationTO) {
        if (gameTypePlayingRelationTO != null) {
            gameTypePlayingRelationTO.setGameLotteryApiUrl("");
            // 彩种游戏剩余倒计时
            CurrentGameIssue gameIssue = gameTypePlayingRelationTO.getCurrentIssue();
            if (null != gameIssue){
                gameTypePlayingRelationTO.setGameRemainingCountdown(gameIssue.getDuration() - gameTypePlayingRelationTO.getClosedTime());
            }else{
                gameTypePlayingRelationTO.setGameRemainingCountdown(0L);
            }
            if (StringUtil.isNotBlank(gameTypePlayingRelationTO.getGameTypePlayingIcon())) {
                gameTypePlayingRelationTO.setGameTypePlayingIcon(getImageServer() + gameTypePlayingRelationTO.getGameTypePlayingIcon());
            }
        }
        log.info("游戏剩余时间=========="+gameTypePlayingRelationTO.getGameRemainingCountdown());
        return gameTypePlayingRelationTO;
    }

    private String getImageServer() {
        if (imageServer != null) {
            return imageServer;
        }
        return imageServer = sysDictService.findImageServerAddress();
    }

}
